package com.example.kliven.because.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.example.kliven.because.R;
import com.example.kliven.because.bean.RankingBean;
import com.example.kliven.because.imagecache.DoubleCache;

import java.util.List;

/**
 * Created by Kliven on 16/1/12.
 */
public class RankingAdapter extends RecyclerView.Adapter<RankingAdapter.RankingViewHodler> {

    private Context context;
    private List<RankingBean> rankingBeans;
    private RequestQueue queue;

    public RankingAdapter(Context context, List<RankingBean> rankingBeans) {
        this.context = context;
        this.rankingBeans = rankingBeans;
    }

    @Override
    public RankingViewHodler onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_ranking,parent,false);
        RankingViewHodler rankingViewHodler = new RankingViewHodler(view);
        return rankingViewHodler;
    }

    @Override
    public void onBindViewHolder(RankingViewHodler holder, int position) {
        holder.tv_member_name.setText(rankingBeans.get(position).getMember_name());
        holder.tv_shop_name.setText(rankingBeans.get(position).getShop_name());
        holder.tv_dish_name.setText(rankingBeans.get(position).getDish_name());
        holder.tv_time_tips.setText(rankingBeans.get(position).getTime_tips());
        holder.tv_award_count.setText(rankingBeans.get(position).getAward_count());

        queue = Volley.newRequestQueue(context);
        // 5. 初始化缓存
        DoubleCache cache = new DoubleCache();
        // 4. 初始化ImageLoager
        ImageLoader imageLoader = new ImageLoader(queue, cache);
        // 1. 设置默认图片;
        holder.netWork_ico.setDefaultImageResId(R.mipmap.moren);
        holder.network_main_photo.setDefaultImageResId(R.mipmap.error_img);
        // 2. 设置Error图片;
        holder.netWork_ico.setErrorImageResId(R.mipmap.moren);
        holder.network_main_photo.setErrorImageResId(R.mipmap.error_img);
        // 3. 加载网络图片;
        holder.netWork_ico.setImageUrl(rankingBeans.get(position).getIco(), imageLoader);
        holder.network_main_photo.setImageUrl(rankingBeans.get(position).getMain_photo_url(), imageLoader);
    }

    @Override
    public int getItemCount() {
        return rankingBeans.size();
    }

    class RankingViewHodler extends RecyclerView.ViewHolder {

        TextView tv_member_name;
        TextView tv_shop_name;
        TextView tv_dish_name;
        TextView tv_time_tips;
        TextView tv_award_count;

        NetworkImageView netWork_ico,network_main_photo;
        public RankingViewHodler(View itemView) {
            super(itemView);
            tv_member_name = (TextView) itemView.findViewById(R.id.member_name);
            tv_shop_name = (TextView) itemView.findViewById(R.id.shop_name);
            tv_dish_name = (TextView) itemView.findViewById(R.id.dish_name);
            tv_time_tips = (TextView) itemView.findViewById(R.id.time_tips);
            tv_award_count = (TextView) itemView.findViewById(R.id.award_count);
            netWork_ico = (NetworkImageView) itemView.findViewById(R.id.netWork_ico);
            network_main_photo = (NetworkImageView) itemView.findViewById(R.id.network_main_photo);
        }
    }
}
